package com.yyt.securitydemo.controller;

import com.yyt.securitydemo.dao.UserDao;
import com.yyt.securitydemo.domain.MyUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author yeyangtao
 * @since 2022/7/27 15:00
 */
@RestController
public class UserController {

    @Resource
    private UserDao userDao;

    @GetMapping("login")
    public String login(@RequestParam String name, @RequestParam String passwd) {
        return "successful";
    }

    @PostMapping("register")
    public String register(@RequestParam String name, @RequestParam String passwd) {
        MyUser myUser = new MyUser(name,passwd, Collections.singletonList(new SimpleGrantedAuthority("base")));
        userDao.add(myUser);
        return "successful";
    }

    @GetMapping("test")
    @PreAuthorize("hasAnyAuthority('base')")
    public String test(){
        return "test";
    }
}
